package com.kaishengit.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.kaishengit.pojo.User;
import com.kaishengit.service.UserService;

public class UserAction implements SessionAware{
	private User user;
	private Map<String, Object> session;
	
	private UserService us;
	
	@Autowired
	public void setUs(UserService us) {
		this.us = us;
	}

	@Action("toLogin")
	public String execute(){
		return "success";
	}
	
	@Action(value="login",results={
			@Result(name="success",type="redirectAction",params={"actionName","home"}),
			@Result(name="login",type="redirectAction",params={"actionName","toLogin"})
	})
	public String login(){
		User u = us.findByName(user);
		
		if(u != null) {
			session.put("cur_user", u);
			List<User> userList = us.findAll(u);
			session.put("userList", userList);
			return "success";
		} else {
			return "login";
		}
		
	}

	//get set
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	
	
}
